package rhinestone.Model;

import java.util.Iterator;

/**
 * Calculates the statistic of employee or customer.
 *
 * @author Group 2
 * @version 2010.12.24
 */
public class StatisticCalculator {
	private SaleContainer saleContainer;

	public StatisticCalculator(SaleContainer saleContainer) {
		this.saleContainer = saleContainer;
	}

	public String getStatistic(Staff staff) {
		double priceSold = 0;
		int productCount = 0;
		Iterator<Sale> it = saleContainer.getSales(staff);
		while(it.hasNext()) {
			Sale sale = it.next();
			productCount += sale.getItemCount();
			priceSold += sale.getTotalPrice();
		}
		return makeStatistic(staff, productCount, priceSold);
	}

	private String makeStatistic(Staff staff, int productCount, double priceSold) {
		String output = " ----- STATISTIC ------";
		output += "\nEmployee CPR: " + staff.getCpr();
		output += "\nEmployee name: " + staff.getName();
		output += "\n----------------------------------------";
		output += "\nTotal products sold: " + productCount;
		output += "\nTotal sold product value: $" + priceSold;
		output += "\n----------------------------------------";
		return output;
	}

	public String getStatistic(Customer customer) {
		double moneySpent = 0;
		int productsBought = 0;
		Iterator<Sale> it = saleContainer.getSales(customer);
		while(it.hasNext()) {
			Sale sale = it.next();
			productsBought += sale.getItemCount();
			moneySpent += sale.getTotalPrice();
		}
		return makeStatistic(customer, productsBought, moneySpent);
	}

	private String makeStatistic(Customer customer, int productsBought, double moneySpent) {
		String output = " ----- STATISTIC ------";
		output += "\nCustomer CPR: " + customer.getCpr();
		output += "\nCustomer name: " + customer.getName();
		output += "\n----------------------------------------";
		output += "\nTotal products bought: " + productsBought;
		output += "\nTotal bought product value: $" + moneySpent;
		output += "\n----------------------------------------";
		return output;
	}
}
